import pytest
import requests


class one:
    @pytest.fixture()
    def necessary(self):
        url = "http://192.168.222.132:8080"
        data = {"loginname": "guest", "password": "guest"}
        header = {"Content-Type": "Application/json"}
        yield url, data, header


class Test_login(one):
    def test_login(self, necessary):
        url = necessary[0] + "/v7/login"
        data = necessary[1]
        data["loginname"] = "zhangsan"
        data["password"] = "123456"
        header = necessary[2]
        res = requests.post(url=url, headers=header, json=data)
        res.encoding = "UTF-8"
        assert res.status_code != 200


class Test_two(one):
    @pytest.fixture
    def login(self, necessary):
        session = requests.session()
        url = necessary[0] + "/v7/login"
        data = necessary[1]
        header = necessary[2]
        res = session.post(url=url, headers=header, json=data)
        res.encoding = "UTF-8"
        body = res.json()
        token = body['token']
        header["Authorization"] = f"Bearer {token}"
        yield necessary[0], header

    def test_pimpeople(self, login):
        url = login[0] + "/pimpeople"
        header = login[1]
        data = {
            "pimpersonname": "张三2",
            "ygbh": "123456",
            "zjlx": "10",
            "zjhm": "123456",
            "yhzt": "10",
            "lxdh": "13900000000"
        }
        res_pim = requests.post(url=url, headers=header, json=data)
        res_pim.encoding = "UTF-8"
        code = res_pim.status_code
        assert code == 200

    def test_upload(self, login):
        url = login[0] + "/ibizutil/upload"
        header = login[1]
        del header['Content-Type']
        files = {'file': open('D:\\1\\A.jpeg', 'rb')}
        res_upload = requests.post(url=url, files=files, headers=header)
        res_upload.encoding = "UTF-8"
        code = res_upload.status_code
        assert code == 200

    def test_save(self, login):
        url = login[0] + "/pimpeople/a5284e000ca4ead198ebbcecf390f10f"
        header = login[1]
        data = {
            "lxdh": "13901472583690000"

        }
        res = requests.put(url=url, headers=header, json=data)
        res.encoding = "UTF-8"
        code = res.status_code
        assert code == 200

    def test_fami(self, login):
        url = login[0] + "/pimpeople/a5284e000ca4ead198ebbcecf390f10f/pimfaminfos"
        header = login[1]
        data = {
            "pimfaminfoname": "Python",
            "ybrgx": "50"
        }
        res = requests.post(url=url, headers=header, json=data)
        res.encoding = "UTF-8"
        code = res.status_code
        assert code == 200

    def test_reward(self, login):
        url = login[0] + "/pimpeople/a5284e000ca4ead198ebbcecf390f10f/pimrewardpunishments"
        header = login[1]
        data = {
            "pimrewardpunishmentname": "Python",
            "lx": "REWARD",
            "jcjb": "20",
            "jlsj": "2024-07-01",
            "department": "2"
        }
        res = requests.post(url=url, headers=header, json=data)
        res.encoding = "UTF-8"
        code = res.status_code
        assert code == 200
